﻿@model HANS_BackStage.Model.DbModel.Backstage_role
@{
    ViewBag.Title = "创建角色";

}
@section Scripts{
    <script type="text/javascript">
        $(function () {
            function getPermissionTree() {
                var tree=@Html.Raw(ViewBag.PermissionJsonTree);
                return tree;
            }

            //$('#permissionTree').treeview({
            //    data: getPermissionTree() 
            //});

            $('#permissionTree').treeview({
                data: getPermissionTree(),
                showCheckbox: true,
                highlightSelected: false,
                //递归选择父节点
                onNodeChecked: function (event, data) {
                    var that = this;
                    function checkParentNode(note){
                        if (note.parentId !==undefined) {
                            var parentNote = $(that).treeview('getNode', note.parentId);
                            $(that).treeview('checkNode', parentNote.nodeId);
                            checkParentNode(parentNote);
                        }
                    }
                    checkParentNode(data);
                },
                //递归取消子节点选择
                onNodeUnchecked: function (event, data) {
                    var that = this;
                    function uncheckChindrenNode(nodes) {
                        if (nodes && nodes.length) {
                            $.each(nodes, function (i, v) {
                                $(that).treeview('uncheckNode', v.nodeId);
                                uncheckChindrenNode(v.nodes);
                            });
                        }
                    }
                    uncheckChindrenNode(data.nodes);
                }
            });
            
            window.beforeAjaxFormSubmit = function(){
                debugger
                var selectPermissionIds = [];
                var selectNode = $('#permissionTree').treeview('getChecked');
                for(var i = 0; i< selectNode.length; i++){
                    selectPermissionIds.push(selectNode[i].serverId);
                }
                $("#PermissionId").val(selectPermissionIds.join(","));
            }
        });
    </script>    
}
<div class="x_panel">
    <!--beginHead-->
    <div class="x_title">
        <h2>创建角色 <small>HANS</small></h2>
        <ul class="nav navbar-right panel_toolbox">
            <li>
                <a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
            </li>
        </ul>
        <div class="clearfix"></div>
    </div>
    <!--endHead-->
    <div class="x_content">
        <!--beginForm-->
        @using (Html.BeginForm("CreateRole", "Account", FormMethod.Post,
                                                            new { @class = "form-horizontal form-label-left js-form-submit", @novalidate = "" }))
        {
            @Html.AntiForgeryToken()
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">
                    名称<span class="required">*</span>
                </label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    @Html.TextBoxFor(i => i.Rolename, new { @class = "form-control col-md-7 col-xs-12", @placeholder = "中文" })
                </div>
                @Html.ValidationMessageFor(i => i.Rolename, null, new { @class = "text-danger" })
                
            </div>
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">
                    描述<span class="required">*</span>
                </label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    @Html.TextAreaFor(i => i.Description, new { @class = "form-control col-md-7 col-xs-12" })
                </div>

            </div>
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">
                    权限<span class="required">*</span>
                </label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                   <div id="permissionTree"></div>
                    @Html.Hidden("PermissionId")
                </div>

            </div>

            <div class="ln_solid"></div><!--一条横线-->
            <div class="form-group">
                <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                    <button type="submit" class="btn btn-success">保存</button>
                    <a href="@HttpContext.Current.Request.UrlReferrer" class="btn btn-default">取消</a>
                </div>
            </div>
        }
        <!--endForm-->
    </div>
</div>

